library(gplots)
par(mfrow=c(1,2),mar=c(3,3.2,1.3,1),oma=rep(.5,4),mgp=c(2,1,0))
f<-function(z) {
  z<-gsub("(highest)","",z,fixed=TRUE)
  z<-gsub("(lowest)","",z,fixed=TRUE)
  as.numeric(z)
}
x<-read.table("topics.txt",sep="&")
x<-x[nrow(x):1,]
barplot2(x[,3],names.arg=f(x[,1]),col='blue',xlab="Income Decile",ylab=expression(R^2))
mtext(side=3,line=0,"Topics")
x<-read.table("liwc.txt",sep="&")
x<-x[nrow(x):1,]
barplot2(x[,2],names.arg=f(x[,1]),col='red',xlab="Income Decile",ylab=expression(R^2))
mtext(side=3,line=0,"Dictionary")
f2<-function(z) {
  z<-strsplit(z,",",fixed=TRUE)
  z1<-sapply(z,"[",1)
  z1<-as.numeric(gsub("[","",z1,fixed=TRUE))
  z2<-sapply(z,"[",2)
  z2<-strsplit(z2,"]",fixed=TRUE)
  z2<-as.numeric(sapply(z2,"[",1))
  cbind(z1,z2)
}
pdf("decile_barplots.pdf",width=6,height=3)
par(mfrow=c(1,2),mar=c(3,3.2,1.3,1),oma=rep(.5,4),mgp=c(2,1,0))
x<-read.table("topics.txt",sep="&")
x<-x[nrow(x):1,]
col<-col2rgb("blue")
col<-rgb(col[1],col[2],col[3],alpha=55,max=255)
barplot2(x[,3],names.arg=f(x[,1]),col=col,ylim=c(0,.47),
         plot.ci=TRUE,ci.l=f2(x[,4])[,1],ci.u=f2(x[,4])[,2],ci.width=.75,
         cex.axis=.7,cex.names=.7,
         xlab="Income Decile",ylab=expression(R^2)
)
mtext(side=3,line=0,"Topics")
x<-read.table("liwc.txt",sep="&")
x<-x[nrow(x):1,]
col<-col2rgb("red")
col<-rgb(col[1],col[2],col[3],alpha=55,max=255)
barplot2(x[,2],names.arg=f(x[,1]),col=col,ylim=c(0,.47),
         plot.ci=TRUE,ci.l=f2(x[,3])[,1],ci.u=f2(x[,3])[,2],ci.width=.75,
         cex.axis=.7,cex.names=.7,
         xlab="Income Decile",ylab=expression(R^2)
)
mtext(side=3,line=0,"Dictionary")
dev.off()